package logic

import (
	"context"
	"doctor/models"
	"fmt"

	"doctor/doctorrpc/doctorrpc"
	"doctor/doctorrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改
func (l *UpdateLogic) Update(in *doctorrpc.UpdateRequest) (*doctorrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	var doctor models.Doctor
	models.DB.Debug().Where("id = ?", in.DoctorId).Find(&doctor)
	if doctor.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	doctor = models.Doctor{
		Name:  in.Name,
		Image: in.Image,
		Job:   in.Job,
		Age:   int(in.Age),
	}

	models.DB.Debug().Updates(&doctor)

	return &doctorrpc.UpdateResponse{
		DoctorId: int64(doctor.ID),
	}, nil
}
